package com.test.base64;

import com.test.utils.SequenceIdGenerator;

import java.util.ArrayList;

public class NumberBaseDemo {

    private static final String DEFAULT_CHARACTER_TABLE_STR = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    private static final char[] DEFAULT_CHARACTER_TABLE = DEFAULT_CHARACTER_TABLE_STR.toCharArray();

    public static void main(String[] args) {

//        System.out.println(DEFAULT_CHARACTER_TABLE.length);
//        long n = 9999999999L;
//        String s = math10to64(n);
//        math64To10(s);
//
//        String encode = encode(n);
//        System.out.println(encode);
//        System.out.println(decode(encode));
        generate();
    }


    public static void math10To16() {
        int n = 165692981;
        String[] sum = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
        ArrayList<String> list = new ArrayList<String>();
        if (n == 0) System.out.println(0);
        int yu;
        while (n != 0) {
            yu = n % 16;
            n = n / 16;
            list.add(sum[yu]);
        }
        String str = "";
        for (int i = list.size() - 1; i >= 0; i--)
            str += list.get(i);
        System.out.println(str);
    }

    /**
     * 数学方式10进制转64进制
     *
     * @param n
     * @return
     */
    public static String math10to64(long n) {
        ArrayList<Character> list = new ArrayList<>();
        long yu;
        while (n != 0) {
            yu = n % 62;
            n = n / 62;
            list.add(DEFAULT_CHARACTER_TABLE[(int) yu]);
        }
        String str = "";
        for (int i = list.size() - 1; i >= 0; i--) {
            str += list.get(i);
        }
//        System.out.println(str);
        return str;
    }

    /**
     * 数学方式64进制转10进制
     *
     * @param strNum
     */
    public static void math64To10(String strNum) {
        char[] chars = strNum.toCharArray();
        long result = 0L;
        for (int i = 0; i < chars.length; i++) {
            int idx = findIndex(chars[i]);
            int pow = chars.length - 1 - i;
            result += power(64L, pow) * idx;
        }
        System.out.println(result);
    }


    private static int findIndex(char c) {
        return DEFAULT_CHARACTER_TABLE_STR.indexOf(c);
    }

    /**
     * 幂运算
     *
     * @param num
     * @param pow
     * @return
     */
    private static long power(long num, int pow) {
        long ret = 1L;
        for (int i = 0; i < pow; i++) {
            ret *= num;
        }
        return ret;
    }

    /**
     * 位运算方式10进制转64进制
     *
     * @param d10
     * @return
     */
    public static String encode(long d10) {
        StringBuilder s = new StringBuilder();
        while (d10 != 0) {
            s.append(DEFAULT_CHARACTER_TABLE_STR.charAt((int) (d10 & 0b111111)));
            d10 = d10 >>> 6;
        }
        return s.reverse().toString();
    }

    /**
     * 位运算方式64进制转10进制
     *
     * @param s64
     * @return
     */
    public static long decode(String s64) {
        long r = 0;
        for (int i = 0; i < s64.length(); i++) {
            r += (long) DEFAULT_CHARACTER_TABLE_STR.indexOf(s64.charAt(s64.length() - 1 - i)) << 6 * i;
        }
        return r;
    }

    public static void generate() {
        SequenceIdGenerator sequenceIdGenerator = new SequenceIdGenerator(10, 15);

        for (int i = 0; i < 150; i++) {
            long id = sequenceIdGenerator.nextId();

            System.out.println(math10to64(id));
        }
    }
}
